#include "StdAfx.h"
#include "SystemButton.h"

CSystemButton::CSystemButton(void):m_imgButton(NULL),m_bMouseTracking(false),m_bButtonState(BUTTON_DEFAULT)
{
}

CSystemButton::~CSystemButton(void)
{
	if(m_imgButton)
		::DeleteObject(m_imgButton);
}

void CSystemButton::DoPaint(HDC hDC, const RECT& rcPaint)
{
	HDC memdc = ::CreateCompatibleDC(hDC);
	HBITMAP bmpTemp = (HBITMAP)::SelectObject(memdc,m_imgButton);
	int nX = 0;
	int nY = 0;
	int nWidth = m_imgInfo.bmWidth/4;
	int nHeight = m_imgInfo.bmHeight;
	if(m_bButtonState == BUTTON_MOUSEHOVER)
	{
		nX = nWidth;
	}
	else if(m_bButtonState == BUTTON_KEYDOWN)
	{
		nX = nWidth * 2;
	}
	::StretchBlt(hDC,0,0,rcPaint.right - rcPaint.left,rcPaint.bottom - rcPaint.top, 
		memdc, nX, nY,nWidth,nHeight, SRCCOPY);
	::SelectObject(memdc, bmpTemp);
	::DeleteDC(memdc);
}

void CSystemButton::CreateButton(LPCTSTR strResource,HWND hWndParent,HINSTANCE hInst,const RECT rc)
{
	m_hWndParent = hWndParent;
	m_rcItem = rc;
	if(m_imgButton)
		::DeleteObject(m_imgButton);
	m_imgButton = (HBITMAP) ::LoadImage(hInst,strResource/*MAKEINTRESOURCE(IDB_BITMAP_MINIMIZE)*/, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
	ASSERT(m_imgButton!=NULL);
	if( m_imgButton == NULL ) return;
	::GetObject(m_imgButton, sizeof(BITMAP), &m_imgInfo);

	m_hWnd = Create(hWndParent,L"SystemButton",WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,WS_EX_TOOLWINDOW,rc);

}

LRESULT CALLBACK CSystemButton::WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT ps = {0};
			HDC hDC = ::BeginPaint(m_hWnd,&ps);
			DoPaint(hDC,m_rcItem);
			::EndPaint(m_hWnd,&ps);
			break;
		}
	case WM_MOUSEMOVE:
		{
			if( !m_bMouseTracking ) {
				TRACKMOUSEEVENT tme = { 0 };
				tme.cbSize = sizeof(TRACKMOUSEEVENT);
				tme.dwFlags = TME_HOVER | TME_LEAVE;
				tme.hwndTrack = hWnd;
				tme.dwHoverTime = 10;
				_TrackMouseEvent(&tme);
				m_bMouseTracking = true;
			}
			break;
		}
	case WM_MOUSEHOVER:
		{
			m_bButtonState = BUTTON_MOUSEHOVER;
			Invalidate();
			break;
		}
	case WM_MOUSELEAVE:
		{
			m_bMouseTracking = false;
			m_bButtonState = BUTTON_DEFAULT;
			Invalidate();
			break;
		}
	case WM_LBUTTONDOWN:
		{
			m_bButtonState = BUTTON_KEYDOWN;
			Invalidate();
			break;
		}
	case WM_LBUTTONUP:
		{
			POINT pt ={0};
			GetCursorPos(&pt);
			RECT rc;
			GetWindowRect(hWnd,&rc);
			//ScreenToClient(hWnd,&pt);
			if(PtInRect(&rc,pt))
			{
				OnMouseUp();
				m_bButtonState = BUTTON_KEYUP;
				Invalidate();
			}
			
			break;
		}
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
}

void CSystemButton::OnMouseHover()
{

}
void CSystemButton::OnMouseLeave()
{

}

void CSystemButton::OnMouseUp()
{
	if(m_hWnd && m_hWndParent)
	{
		::SendMessage(m_hWndParent,FRAMEWINDOWS_MINIMIZE,0,0);
	}
}

void CSystemButton::Invalidate()
{
	if(m_hWnd)
	{
		RECT rcClient;
		GetClientRect(m_hWnd,&rcClient);
		::InvalidateRect(m_hWnd,&rcClient,TRUE);
		//DoPaint(HDC hDC, const RECT& rcPaint);

	}
}
